<?php
class hotel_sys_cls{

	private  $server_url;
	private	 $version = '20111128102912';							//接口协议版本号，详见接口协议文档
	private	 $accountID = '0e6d6d3d-2f19-4554-9a67-ed0f2cea07d0';	//API帐户ID(小写)，待申请审批通过后发
	private	 $accountKey = '87f7a9b67e94d856';		//API帐户密钥，待申请审批通过后发放



	public function  __construct() {
		$this->server_url = 'http://tcopenapi.17usoft.com/handlers/hotel/QueryHandler.ashx';
	}

	function concat_string($array) {
		$arg  = "";
		while (list ($key, $val) = each ($array)) {
			$arg.=$key."=".$val."&";
		}
		$arg = substr($arg,0,count($arg)-2); //去掉最后一个&字符
		return $arg;
	}

	/**
	 * 对数组排序
	 * $array 排序前的数组
	 * return 排序后的数组
	 */
	function arg_sort($array) {
		ksort($array);
		reset($array);
		return $array;
	}

	/**
	 * 实现多种字符编码方式
	 * $input 需要编码的字符串
	 * $_output_charset 输出的编码格式
	 * $_input_charset 输入的编码格式
	 * return 编码后的字符串
	 */
	function charset_encode($input,$_output_charset ,$_input_charset) {
		$output = "";
		if(!isset($_output_charset))$_output_charset = $_input_charset;
		if($_input_charset == $_output_charset || $input ==null ) {
			$output = $input;
		} elseif (function_exists("mb_convert_encoding")) {
			$output = mb_convert_encoding($input,$_output_charset,$_input_charset);
		} elseif(function_exists("iconv")) {
			$output = iconv($_input_charset,$_output_charset,$input);
		} else die("sorry, you have no libs support for charset change.");
		return $output;
	}

	/**
	 * 实现多种字符解码方式
	 * $input 需要解码的字符串
	 * $_output_charset 输出的解码格式
	 * $_input_charset 输入的解码格式
	 * return 解码后的字符串
	 */
	public function charset_decode($input,$_input_charset ,$_output_charset) {
		$output = "";
		if(!isset($_input_charset) )$_input_charset = $_input_charset ;
		if($_input_charset == $_output_charset || $input ==null ) {
			$output = $input;
		} elseif (function_exists("mb_convert_encoding")) {
			$output = mb_convert_encoding($input,$_output_charset,$_input_charset);
		} elseif(function_exists("iconv")) {
			$output = iconv($_input_charset,$_output_charset,$input);
		} else die("sorry, you have no libs support for charset changes.");
		return $output;
	}

    private  function get_xml_data($xml)
	{	    
		$header = array();
		$header[] = "Content-type: text/xml";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->server_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
		$response = curl_exec($ch);
		if(curl_errno($ch))
		{
			return curl_error($ch);
		}
		curl_close($ch);
		header("Content-type: text/xml");
		return $response;
	}

	/**
	 * 获取酒店列表 
	 */
	public function get_hotel_list(){
		date_default_timezone_set('PRC');
		$version = $this->version;
		$accountID = $this->accountID;
		$accountKey = $this->accountKey;
		$serviceName = 'GetHotelList';							//调用接口的方法名称
		$currentMS =  (int)(microtime()*1000); 					//当前时间的毫秒
		$reqTime = date("Y-m-d H:i:s").".".$currentMS;			//当前时间到毫秒
		$arr = array('Version'=>$version,
				'AccountID' => $accountID,
				'ServiceName' => $serviceName,
				'ReqTime' => $reqTime
		);
		$sort_array  = $this->arg_sort($arr);
		$arg = $this->concat_string($sort_array);
		$digitalSign = md5($arg.$accountKey); //数字签名
		$xml_data = '<?xml version="1.0" encoding="utf-8"?>
			<request>
				<header>
					<version>'.$version.'</version>
					<accountID>'.$accountID.'</accountID>
					<serviceName>'.$serviceName.'</serviceName>
					<digitalSign>'.$digitalSign.'</digitalSign>
					<reqTime>'.$reqTime.'</reqTime>
				</header>
				<body>
					<cityId>80</cityId>
					<sectionId></sectionId>
					<bizSectionId></bizSectionId>
					<comeDate>2012-12-24</comeDate>
					<leaveDate>2012-12-25</leaveDate>
					<searchFields></searchFields>
					<keyword></keyword>
					<radius></radius>
					<priceRange></priceRange>
					<facilityId></facilityId>
					<themeId></themeId>
					<chainId></chainId>
					<starRatedId></starRatedId>
					<sortType></sortType>
					<cs></cs>
					<latitude></latitude>
					<longitude></longitude>
					<chainFlag></chainFlag>
					<ifPDB></ifPDB>
					<hbs></hbs>
					<clientIp>'.pl_onlineip().'</clientIp>
					<page></page>
					<pageSize></pageSize>
				</body>
			</request>';
			return $this->get_xml_data($xml_data);
	}

    /**
	 *
	 * 获取酒店详情 GetHotelDetail
	 */
	public function get_hotel_detail($hotelId)
	{
		date_default_timezone_set('PRC');
		$version = $this->version;
		$accountID = $this->accountID;
		$accountKey = $this->accountKey;
		$serviceName = 'GetHotelDetail';							//调用接口的方法名称
		$currentMS =  (int)(microtime()*1000); 					//当前时间的毫秒
		$reqTime = date("Y-m-d H:i:s").".".$currentMS;			//当前时间到毫秒
		$arr = array('Version'=>$version,
				'AccountID' => $accountID,
				'ServiceName' => $serviceName,
				'ReqTime' => $reqTime
		);
		$sort_array  = $this->arg_sort($arr);
		$arg = $this->concat_string($sort_array);
		$digitalSign = md5($arg.$accountKey); //数字签名
		$xml_data = '<?xml version="1.0" encoding="utf-8"?>
					<request>
					  <header>
					    <version>'.$version.'</version>
						<accountID>'.$accountID.'</accountID>
						<serviceName>'.$serviceName.'</serviceName>
						<digitalSign>'.$digitalSign.'</digitalSign>
						<reqTime>'.$reqTime.'</reqTime>
					  </header>
					  <body>
						<hotelId>'.$hotelId.'</hotelId>
						<cs></cs>
					  </body>
					</request>';
			return $this->get_xml_data($xml_data);	
	}

    /**
	 * 获取房型信息 GetHotelRooms
	 */
	public function get_hotel_rooms($hotelId, $comeDate, $leaveDate){
		date_default_timezone_set('PRC');
		$version = $this->version;
		$accountID = $this->accountID;
		$accountKey = $this->accountKey;
		$serviceName = 'GetHotelRooms';							//调用接口的方法名称
		$currentMS =  (int)(microtime()*1000); 					//当前时间的毫秒
		$reqTime = date("Y-m-d H:i:s").".".$currentMS;			//当前时间到毫秒
		$arr = array('Version'=>$version,
				'AccountID' => $accountID,
				'ServiceName' => $serviceName,
				'ReqTime' => $reqTime
		);
		$sort_array  = $this->arg_sort($arr);
		$arg = $this->concat_string($sort_array);
		$digitalSign = md5($arg.$accountKey); //数字签名
		$xml_data = '<?xml version="1.0" encoding="utf-8"?>
				<request>
				  <header>
					<version>'.$version.'</version>
					<accountID>'.$accountID.'</accountID>
					<serviceName>'.$serviceName.'</serviceName>
					<digitalSign>'.$digitalSign.'</digitalSign>
					<reqTime>'.$reqTime.'</reqTime>
				  </header>
				  <body>				
					<hotelId>'.$hotelId.'</hotelId>
					<comeDate>'.$comeDate.'</comeDate>
					<leaveDate>'.$leaveDate.'</leaveDate>
					<page></page>
					<pageSize></pageSize>
					<roomTypeId></roomTypeId>
					<pricePolicyId></pricePolicyId>
					<defaultPolicyFlag></defaultPolicyFlag>
					<recordCount></recordCount>
					<isSort></isSort>
				  </body>
				</request>';
		return $this->get_xml_data($xml_data);	
	}

	
	/**
	 * 获取多个酒店房型信息 GetHotelRoomsMulti
	 */
	public function get_hotel_rooms_multi($hotelId,$comeDate, $leaveDate)
	{
		date_default_timezone_set('PRC');
		$version = $this->version;
		$accountID = $this->accountID;
		$accountKey = $this->accountKey;
		$serviceName = 'GetHotelRoomsMulti';							//调用接口的方法名称
		$currentMS =  (int)(microtime()*1000); 					//当前时间的毫秒
		$reqTime = date("Y-m-d H:i:s").".".$currentMS;			//当前时间到毫秒
		$arr = array('Version'=>$version,
				'AccountID' => $accountID,
				'ServiceName' => $serviceName,
				'ReqTime' => $reqTime
		);
		$sort_array  = $this->arg_sort($arr);
		$arg = $this->concat_string($sort_array);
		$digitalSign = md5($arg.$accountKey); //数字签名		
		$xml_data = '<?xml version="1.0" encoding="utf-8"?>
					<request>
					  <header>
					    <version>'.$version.'</version>
						<accountID>'.$accountID.'</accountID>
						<serviceName>'.$serviceName.'</serviceName>
						<digitalSign>'.$digitalSign.'</digitalSign>
						<reqTime>'.$reqTime.'</reqTime>
					  </header>
					  <body>
						<hotelId>'.$hotelId.'</hotelId>
						<comeDate>'.$comeDate.'</comeDate>
						<leaveDate>'.$leaveDate.'</leaveDate>
						<page></page>
						<pageSize></pageSize>
					</body>
					</request>';
			return $this->get_xml_data($xml_data);
	}

	/**
	 * 获取酒店房型预订策略GetHotelBookingPolicy
	 */
	public function get_hotel_booking_policy($roomTypeId, $comeDate, $pricePolicyId){
		date_default_timezone_set('PRC');
		$version = $this->version;
		$accountID = $this->accountID;
		$accountKey = $this->accountKey;
		$serviceName = 'GetHotelBookingPolicy';							//调用接口的方法名称
		$currentMS =  (int)(microtime()*1000); 					//当前时间的毫秒
		$reqTime = date("Y-m-d H:i:s").".".$currentMS;			//当前时间到毫秒
		$arr = array('Version'=>$version,
				'AccountID' => $accountID,
				'ServiceName' => $serviceName,
				'ReqTime' => $reqTime
		);
		$sort_array  = $this->arg_sort($arr);
		$arg = $this->concat_string($sort_array);
		$digitalSign = md5($arg.$accountKey); //数字签名
		$xml_data = '<?xml version="1.0" encoding="utf-8"?>
				<request>
				  <header>
					<version>'.$version.'</version>
					<accountID>'.$accountID.'</accountID>
					<serviceName>'.$serviceName.'</serviceName>
					<digitalSign>'.$digitalSign.'</digitalSign>
					<reqTime>'.$reqTime.'</reqTime>
				  </header>
				  <body>
				    <roomTypeId>'.$roomTypeId.'</roomTypeId>
				    <comeDate>'.$comeDate.'</comeDate>
				    <leaveDate></leaveDate>
				    <comeTime></comeTime>
			        	<roomCount></roomCount>
				    <pricePolicyId>'.$pricePolicyId.'</pricePolicyId>
				  </body>
				</request>';
		return $this->get_xml_data($xml_data);
	}

	/**
	 *获取酒店房型担保政策GetGuaranteePolicy
	 */
	public function get_guarantee_policy($roomTypeId, $comeDate){
		date_default_timezone_set('PRC');
		$version = $this->version;
		$accountID = $this->accountID;
		$accountKey = $this->accountKey;
		$serviceName = 'GetGuaranteePolicy';							//调用接口的方法名称
		$currentMS =  (int)(microtime()*1000); 					//当前时间的毫秒
		$reqTime = date("Y-m-d H:i:s").".".$currentMS;			//当前时间到毫秒
		$arr = array('Version'=>$version,
				'AccountID' => $accountID,
				'ServiceName' => $serviceName,
				'ReqTime' => $reqTime
		);
		$sort_array  = $this->arg_sort($arr);
		$arg = $this->concat_string($sort_array);
		$digitalSign = md5($arg.$accountKey); //数字签名
		$xml_data = '<?xml version="1.0" encoding="utf-8"?>
				<request>
				  <header>
					<version>'.$version.'</version>
					<accountID>'.$accountID.'</accountID>
					<serviceName>'.$serviceName.'</serviceName>
					<digitalSign>'.$digitalSign.'</digitalSign>
					<reqTime>'.$reqTime.'</reqTime>
				  </header>
				  <body>
				     <roomTypeId>'.$roomTypeId.'</roomTypeId>
						<comeDate>'.$comeDate.'</comeDate>
				  </body>
				</request>';
		return $this->get_xml_data($xml_data);
	}

	/**
	 *获取房型价格[包含担保政策] GetHotelRoomsWithGuaranteePolicy
	 */
	public function get_hotel_rooms_with_guarantee_policy($hotelId, $comeDate,$leaveDate){
		date_default_timezone_set('PRC');
		$version = $this->version;
		$accountID = $this->accountID;
		$accountKey = $this->accountKey;
		$serviceName = 'GetHotelRoomsWithGuaranteePolicy';
		$currentMS =  (int)(microtime()*1000); 	
		$reqTime = date("Y-m-d H:i:s").".".$currentMS;
		$arr = array('Version'=>$version,
				'AccountID' => $accountID,
				'ServiceName' => $serviceName,
				'ReqTime' => $reqTime
		);
		$sort_array  = $this->arg_sort($arr);
		$arg = $this->concat_string($sort_array);
		$digitalSign = md5($arg.$accountKey); //数字签名
		$xml_data = '<?xml version="1.0" encoding="utf-8"?>
			<request>
				<header>
					<version>'.$version.'</version>
					<accountID>'.$accountID.'</accountID>
					<serviceName>'.$serviceName.'</serviceName>
					<digitalSign>'.$digitalSign.'</digitalSign>
					<reqTime>'.$reqTime.'</reqTime>
				</header>
				<body>
					<hotelId>'.$hotelId.'</hotelId>
					<comeDate>'.$comeDate.'</comeDate>
					<leaveDate>'.$leaveDate.'</leaveDate>
					<page></page>
					<pageSize></pageSize>
					<roomTypeId></roomTypeId>
					<pricePolicyId></pricePolicyId>
					<defaultPolicyFlag></defaultPolicyFlag>
					<recordCount></recordCount>
					<isSort></isSort>
					<isGP></isGP>
					<comeTime></comeTime>
				</body>
			</request>';
		return $this->get_xml_data($xml_data);
	}

	/**
	 *酒店多ID获取返回信息GetHotelInfoMulti
	 */
	public function get_hotel_info_multi($hotelId){
		date_default_timezone_set('PRC');
		$version = $this->version;
		$accountID = $this->accountID;
		$accountKey = $this->accountKey;
		$serviceName = 'GetHotelInfoMulti';
		$currentMS =  (int)(microtime()*1000);
		$reqTime = date("Y-m-d H:i:s").".".$currentMS;
		$arr = array('Version'=>$version,
				'AccountID' => $accountID,
				'ServiceName' => $serviceName,
				'ReqTime' => $reqTime
		);
		$sort_array  = $this->arg_sort($arr);
		$arg = $this->concat_string($sort_array);
		$digitalSign = md5($arg.$accountKey); //数字签名
		$xml_data = '<?xml version="1.0" encoding="utf-8"?>
			<request>
				<header>
					<version>'.$version.'</version>
					<accountID>'.$accountID.'</accountID>
					<serviceName>'.$serviceName.'</serviceName>
					<digitalSign>'.$digitalSign.'</digitalSign>
					<reqTime>'.$reqTime.'</reqTime>
				</header>
				<body>
					<hotelId>'.$hotelId.'</hotelId>
					<coordinateType></coordinateType>
				</body>
			</request>';
		return $this->get_xml_data($xml_data);
	}

	/**
	 *获取酒店交通信息GetHotelTrafficInfo
	 */
	public function get_hotel_trafficinfo($hotelId){
		date_default_timezone_set('PRC');
		$version = $this->version;
		$accountID = $this->accountID;
		$accountKey = $this->accountKey;
		$serviceName = 'GetHotelTrafficInfo';
		$currentMS =  (int)(microtime()*1000);
		$reqTime = date("Y-m-d H:i:s").".".$currentMS;
		$arr = array('Version'=>$version,
				'AccountID' => $accountID,
				'ServiceName' => $serviceName,
				'ReqTime' => $reqTime
		);
		$sort_array  = $this->arg_sort($arr);
		$arg = $this->concat_string($sort_array);
		$digitalSign = md5($arg.$accountKey); //数字签名
		$xml_data = '<?xml version="1.0" encoding="utf-8"?>
			<request>
				<header>
					<version>'.$version.'</version>
					<accountID>'.$accountID.'</accountID>
					<serviceName>'.$serviceName.'</serviceName>
					<digitalSign>'.$digitalSign.'</digitalSign>
					<reqTime>'.$reqTime.'</reqTime>
				</header>
				<body>
					<hotelId>'.$hotelId.'</hotelId>
				</body>
			</request>';
		return $this->get_xml_data($xml_data);
	}

	/**
	 * 获取酒店图片列表GetHotelImageList
	 */
	public function get_hotel_imagelist($hotelId, $page, $pageSize){
		date_default_timezone_set('PRC');
		$version = $this->version;
		$accountID = $this->accountID;
		$accountKey = $this->accountKey;
		$serviceName = 'GetHotelImageList';
		$currentMS =  (int)(microtime()*1000);
		$reqTime = date("Y-m-d H:i:s").".".$currentMS;
		$arr = array('Version'=>$version,
				'AccountID' => $accountID,
				'ServiceName' => $serviceName,
				'ReqTime' => $reqTime
		);
		$sort_array  = $this->arg_sort($arr);
		$arg = $this->concat_string($sort_array);
		$digitalSign = md5($arg.$accountKey); //数字签名
		$xml_data = '<?xml version="1.0" encoding="utf-8"?>
			<request>
				<header>
					<version>'.$version.'</version>
					<accountID>'.$accountID.'</accountID>
					<serviceName>'.$serviceName.'</serviceName>
					<digitalSign>'.$digitalSign.'</digitalSign>
					<reqTime>'.$reqTime.'</reqTime>
				</header>
				<body>
					<hotelId>'.$hotelId.'</hotelId>
			    <page>'.$page.'</page>
			    <pageSize>'.$pageSize.'</pageSize>
				</body>
			</request>';
		return $this->get_xml_data($xml_data);
	}
}

